package com.tanqidi.arcgisandroid.ui.layerConfig.subLayer

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.tanqidi.arcgisandroid.R
import com.tanqidi.arcgisandroid.base.BaseRecyclerAdapter
import com.tanqidi.arcgisandroid.base.RecyclerViewHolder
import com.tanqidi.arcgisandroid.base.utils.showConfirmDialog
import com.tanqidi.arcgisandroid.base.utils.showMenu
import com.tanqidi.arcgisandroid.data.layerConfig.Layer
import com.tanqidi.arcgisandroid.data.project.Project
import com.tanqidi.arcgisandroid.databinding.FragmentSubLayerBinding
import com.tanqidi.arcgisandroid.ui.layerConfig.LayerConfigFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SubLayerFragment : Fragment() {
    private var type: String? = null
    private lateinit var projectName: String
    private lateinit var binding: FragmentSubLayerBinding
    private val subLayerViewModel by viewModels<SubLayerViewModel>()
    private lateinit var layerAdapter: LayerAdapter
    private lateinit var project: Project

    companion object {
        const val LAYER_TYPE = "layerType"
        /**
         * 创建页面
         */
        fun newInstance(type: String, projectName: String) = SubLayerFragment().apply {
            this.projectName = projectName
            arguments = Bundle().apply {
                putString(LAYER_TYPE, type)
            }
        }
    }

    /**
     * 重新从本地project配置文件加载最新数据
     */
    fun flushData() {
        project = subLayerViewModel.getProjectByName(projectName)
        val layers = project.layers
        val filter = layers.filter { it.type == this.type }
        layerAdapter.data = filter

        //将数量发送到父类更新tab
        val layerConfigFragment = requireParentFragment() as LayerConfigFragment
        layerConfigFragment.flushTabCount(type!!, filter.size)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let { type = it.getString(LAYER_TYPE) }
        binding = FragmentSubLayerBinding.inflate(layoutInflater)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        initTopBar()
        initViewEvent()
        initLiveData()
        initRecycleView()
        return binding.root
    }

    private fun initRecycleView() {
        val recyclerView = binding.recyclerView
        layerAdapter = LayerAdapter(requireContext(), null)
        layerAdapter.setOnItemClickListener { itemView, pos ->
            val item = layerAdapter.getItem(pos)
            //调用父类的跳转方法跳到 fieldConfig 页面
            val layerConfigFragment = parentFragment as LayerConfigFragment
            layerConfigFragment.goFieldConfigFragment(projectName, item.name)
        }
        recyclerView.adapter = layerAdapter
        //复用一下
        flushData()
    }

    private fun initViewEvent() {

    }

    private fun initTopBar(){

    }

    private fun initLiveData(){

    }

    private inner class LayerAdapter(context: Context, list: List<Layer>?) : BaseRecyclerAdapter<Layer>(context, list) {
        override fun getItemLayoutId(viewType: Int): Int {
            return R.layout.rv_item_layer
        }

        override fun bindData(holder: RecyclerViewHolder, position: Int, item: Layer) {
            holder.getTextView(R.id.title).text = item.name
            holder.getView(R.id.setting).setOnClickListener {
                showMenu(it, R.menu.layer_menu) {
                    when(it.itemId){
                        R.id.delete_layer -> {
                            showConfirmDialog(requireContext(), "提示", "确定删除图层吗") { dialog, which ->
                                project.layers.remove(item)
                                subLayerViewModel.saveProject(project)
                                //复用一下
                                flushData()
                            }
                        }
                    }
                    true
                }
            }
        }

    }


}